package com.passion.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.passion.bean.OfflineAnalyseResult;
import com.passion.bean.OfflineAnalyseResultBar;
import com.passion.mapper.OfflineAnalyseResultMapper;
import com.passion.service.OfflineAnalyseResultBarService;
import com.passion.mapper.OfflineAnalyseResultBarMapper;
import com.passion.service.OfflineAnalysisTemplateSqlTableService;

import com.passion.utils.UUIDUtils;
import lombok.extern.log4j.Log4j;
import org.apache.hadoop.yarn.server.utils.Lock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.passion.enums.CalculateStatus.*;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


import static com.passion.utils.ParseJsonUtil.*;


/**
 * @author 86151
 * @description 针对表【offline_analyse_result_bar(柱状图分析结果表)】的数据库操作Service实现
 * @createDate 2024-10-02 17:16:55
 */
@Log4j
@Service
public class OfflineAnalyseResultBarServiceImpl extends ServiceImpl<OfflineAnalyseResultBarMapper, OfflineAnalyseResultBar>
        implements OfflineAnalyseResultBarService {

    @Autowired
    private OfflineAnalyseResultBarMapper offlineAnalyseResultBarMapper;
    @Autowired
    private OfflineAnalyseResultMapper offlineAnalyseResultMapper;
    @Autowired
    private OfflineAnalysisTemplateSqlTableService offlineAnalysisTemplateSqlTableService;

    @Override
    public JSONObject transformBarResult(Map<String, Object> req) {
        String sqlId = (String) req.get("sqlID");
        String categoriesKey = (String) req.get("categoriesKey");
        String categoriesValue = (String) req.get("categoriesValue");
        // 获取任务ID
        String params1 = (String) req.get("params");
        Map<String, String> map = new LinkedHashMap<>();
        map.put("sqlID", sqlId);
        String[] split = params1.split(",");
        for (String s : split) {
            String[] split1 = s.split(":");
            map.put(split1[0], split1[1]);
        }
        String taskID = UUIDUtils.generateUUID(map);
        log.info("生成uuid为：" + taskID);

        // 查询sqlId对应的离线分析结果
        String resultContent = new JSONObject().toJSONString();
        QueryWrapper<OfflineAnalyseResult> queryWrapper = new QueryWrapper<OfflineAnalyseResult>().eq("sql_id", sqlId).eq("task_id", taskID);
        OfflineAnalyseResult result = offlineAnalyseResultMapper.selectOne(queryWrapper);
        if (result != null) {
            // 将离线分析结果转换为柱状图分析结果并保存到数据库中
            resultContent = result.getAnalyseResult();
        } else {
            // 如果没有离线分析结果，则执行离线分析
            HashMap<String, Object> params = new HashMap<>();
            params.put("type", DIRECT_CALCULATION_GET_RESULTS.getStatus());
            JSONObject jsonObject = new JSONObject();
            jsonObject.putAll(map);
            params.put("params", jsonObject);
            offlineAnalysisTemplateSqlTableService.executeAnalysis(params);
            result = offlineAnalyseResultMapper.selectOne(queryWrapper);
            resultContent = result.getAnalyseResult();
        }
        JSONObject jsonObject = JsonArrayToJsonObject(resultContent, categoriesKey, categoriesValue);
        String taskId = result.getTaskId();
        // 保存柱状图分析结果到数据库中
        offlineAnalyseResultBarMapper.delete(new QueryWrapper<OfflineAnalyseResultBar>().eq("task_id", taskID).eq("sql_id", sqlId));
        offlineAnalyseResultBarMapper.insert(new OfflineAnalyseResultBar(taskId, sqlId, jsonObject.toJSONString()));
        return jsonObject;
    }
}




